package com.atguigu.gmall.sms.service.impl;

import com.atguigu.gmall.sms.entity.SkuFullReductionEntity;
import com.atguigu.gmall.sms.entity.SkuLadderEntity;
import com.atguigu.gmall.sms.mapper.SkuFullReductionMapper;
import com.atguigu.gmall.sms.mapper.SkuLadderMapper;
import com.atguigu.gmall.sms.vo.ItemSaleVo;
import com.atguigu.gmall.sms.vo.SkuSaleVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Id;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.common.bean.PageResultVo;
import com.atguigu.gmall.common.bean.PageParamVo;

import com.atguigu.gmall.sms.mapper.SkuBoundsMapper;
import com.atguigu.gmall.sms.entity.SkuBoundsEntity;
import com.atguigu.gmall.sms.service.SkuBoundsService;
import org.springframework.transaction.annotation.Transactional;


@Service("skuBoundsService")
public class SkuBoundsServiceImpl extends ServiceImpl<SkuBoundsMapper, SkuBoundsEntity> implements SkuBoundsService {

    @Autowired
    SkuLadderMapper skuLadderMapper;

    @Autowired
    SkuFullReductionMapper skuFullReductionMapper;
    @Transactional
    @Override
    public PageResultVo queryPage(PageParamVo paramVo) {
        IPage<SkuBoundsEntity> page = this.page(
                paramVo.getPage(),
                new QueryWrapper<SkuBoundsEntity>()
        );

        return new PageResultVo(page);
    }

    //向各表中添加营销信息数据
    @Override
    public void saveSkuSaleInfo(SkuSaleVo skuSaleVo) {
        if (skuSaleVo==null){
            return;
        }

        //保存积分信息
        SkuBoundsEntity skuboundsEntity = new SkuBoundsEntity();
        BeanUtils.copyProperties(skuSaleVo,skuboundsEntity);
        List<Integer> work = skuSaleVo.getWork();
        int workNum =0;
        if (work!=null){
           workNum = work.get(0)*8+work.get(1)*4+work.get(2)*2+work.get(3);
        }
        skuboundsEntity.setWork(workNum);
        this.save(skuboundsEntity);

        //保存打折信息
        SkuLadderEntity skuladderEntity = new SkuLadderEntity();
        BeanUtils.copyProperties(skuSaleVo,skuladderEntity);
        skuLadderMapper.insert(skuladderEntity);

        //保存满减信息
        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        BeanUtils.copyProperties(skuSaleVo,skuFullReductionEntity);
        skuFullReductionEntity.setAddOther(skuSaleVo.getFullAddOther());
        skuFullReductionMapper.insert(skuFullReductionEntity);


    }

    @Override
    public List<ItemSaleVo> getItemSaleInfoBySkuId(Long skuId) {
        List<ItemSaleVo> itemSaleVoList = new ArrayList<>();

        SkuBoundsEntity skuBoundsEntity = this.getOne(new QueryWrapper<SkuBoundsEntity>().eq("sku_id", skuId));
        if (skuBoundsEntity!=null){
            ItemSaleVo itemSaleVo = new ItemSaleVo();
            itemSaleVo.setType("积分");
            itemSaleVo.setDesc("购买即送购物积分"+skuBoundsEntity.getBuyBounds()+",送成长积分"+skuBoundsEntity.getGrowBounds());
        itemSaleVoList.add(itemSaleVo);
        }
            SkuFullReductionEntity skuFullReductionEntity = this.skuFullReductionMapper.selectOne(new QueryWrapper<SkuFullReductionEntity>().eq("sku_id", skuId));
        if (skuFullReductionEntity!=null){
            ItemSaleVo itemSaleVo = new ItemSaleVo();
            itemSaleVo.setType("满减");
            itemSaleVo.setDesc("满"+skuFullReductionEntity.getFullPrice()+"减"+skuFullReductionEntity.getReducePrice());
        itemSaleVoList.add(itemSaleVo);
        }
            SkuLadderEntity skuLadderEntity = this.skuLadderMapper.selectOne(new QueryWrapper<SkuLadderEntity>().eq("sku_id", skuId));
            if (skuLadderEntity!=null){
                ItemSaleVo itemSaleVo = new ItemSaleVo();
                itemSaleVo.setType("打折");
                itemSaleVo.setDesc("购买"+skuLadderEntity.getFullCount()+"件商品打"+skuLadderEntity.getDiscount().intValue()/10+"折");
                 itemSaleVoList.add(itemSaleVo);
            }
      return itemSaleVoList;
    }

}